package com.xiaotu.util;

import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * @author zhangxiao
 * @create 2022-03-03 16:36
 */
@Slf4j
public class ReplaceWordUtil {

    public static void main(String[] args) {
        /**
         * arg0 文件路径
         * arg1 需要替换的内容
         * arg2 新内容
         */
        replaceTxtByStr("C:\\Users\\Administrator\\Desktop\\tab_city_column_age.csv", new String[]{"{", "}"}, "");
        //iteratorDirectory("D:\\test", "$file$", "test_000");
    }

    /**
     * 遍历文件夹下的所有文件进行文本替换
     *
     * @param fileDir
     * @param oldStr
     * @param replaceStr
     */
    public static void iteratorDirectory(String fileDir, String[] oldStr, String replaceStr) {
        File file = new File(fileDir);
        if (file.isDirectory()) {
            String[] fileList = file.list();
            for (int i = 0; i < fileList.length; i++) {
                iteratorDirectory(fileDir + File.separator + fileList[i], oldStr, replaceStr);
            }
        } else {
            replaceTxtByStr(fileDir, oldStr, replaceStr);
        }
    }

    /**
     * 替换文件中的字符串
     *
     * @param filePath
     * @param oldStr
     * @param replaceStr
     */
    public static void replaceTxtByStr(String filePath, String[] oldStr, String replaceStr) {
        String temp = "";
        //int len = oldStr.length();
        StringBuffer tempBuf = new StringBuffer();
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
            StringBuffer buf = new StringBuffer();


            // 按行读取，仅替换每行第一个匹配的字符串
            //while ((temp = br.readLine()) != null) {
            //    if (temp.contains(oldStr)) {
            //        int index = temp.indexOf(oldStr);
            //        tempBuf.append(temp);
            //        tempBuf.replace(index, index + len, replaceStr);
            //        buf.append(tempBuf);
            //        tempBuf.setLength(0);
            //    } else {
            //        buf.append(temp);
            //    }
            //    buf = buf.append(System.getProperty("line.separator"));
            // }

            // 替换所有匹配的字符串
            for (temp = null; (temp = br.readLine()) != null; temp = null) {
                for (String s : oldStr) {
                    if (temp.indexOf(s) != -1) {
                        temp = temp.replace(s, replaceStr);
                    }
                }
                buf.append(temp);
                buf.append(System.getProperty("line.separator"));
            }

            br.close();
            FileOutputStream fos = new FileOutputStream(file);
            PrintWriter pw = new PrintWriter(fos);
            pw.write(buf.toString().toCharArray());
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
            log.error("修改文件{}失败，错误信息: {}", filePath, e.getMessage());
        }
    }

}
